﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RailwayReservationWeb.manager
{
    public partial class station_edit : System.Web.UI.Page
    {
        private RailwayReservationDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new RailwayReservationDataContext();
            pMessage.Visible = false;
            txtStationCode.ReadOnly = true;
            if (!IsPostBack)
            {
                if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
                {
                    var index = Request.QueryString["id"];
                    var ob = db.Stations.First(p => p.StationCode.Equals(index));
                    if (ob != null)
                    {
                        txtStationCode.Text = ob.StationCode;
                        txtStationName.Text = ob.StationName;
                        txtDivision.Text = ob.Division;
                    }
                }
                else
                {
                    Response.Redirect("~/manager/station_list.aspx");
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (Page.IsValid)
                {
                    var ob = db.Stations.First(p => p.StationCode.Equals(txtStationCode.Text.Trim()));
                    ob.StationName = txtStationName.Text.Trim();
                    ob.Division = txtDivision.Text.Trim();
                    db.SubmitChanges();
                    Response.Redirect("~/manager/station_list.aspx");
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        private void ShowMessage(string msg)
        {
            pMessage.Visible = true;
            pMessage.InnerText = msg;
        }
    }
}
